<template>
  <div>
    <div class="pageTitleWhole">
      <a-breadcrumb>
        <a-breadcrumb-item>销售管理</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <div class="pageContentWhole">
      <a-row>
        <a-col :span="24">
          <a-form layout="inline">
            <a-form-item label="销售姓名">
              <a-input v-model="searchForm.name" />
            </a-form-item>
            <a-form-item label="销售手机号码">
              <a-input v-model="searchForm.account" />
            </a-form-item>

            <a-form-item>
              <a-button type="primary" @click="handleSubmit1">查询</a-button>
            </a-form-item>
            <a-form-item>
              <a-button type="primary" @click="handleAdd">+ 添加销售</a-button>
            </a-form-item>
          </a-form>

          <a-table
            :columns="columns"
            :data-source="tableData"
            class="tableMargin"
            :pagination="pagination"
            rowKey="uiId"
            :loading="loading"
          >
            <template slot="rn" slot-scope="text, record, index">{{index + 1}}</template>
            <span slot="head" slot-scope="record">
              <a-avatar size="large" :src="record.headPortrait || defaultAvatar" />
            </span>
            <template slot="opt" slot-scope="text, record">
              <a @click="removeIt(record.uiId)">移除</a>
            </template>
          </a-table>
        </a-col>
      </a-row>
    </div>
    <sales-person-modal
      :visible="visible"
      @set-visible="setVisible"
      @select-sales-person="selectSalesPerson"
    />
  </div>
</template>

<script>
import { querySellUserListLand, deleteSellExamine, addSellExamine } from '@/api/landCenter'
import ChooseSalesPerson from '@/page/chooseSalesPerson'
import defaultAvatar from '@/../static/images/defaultAvatar.png'

export default {
  data () {
    return {
      defaultAvatar,
      loading: false,
      searchForm: {
        name: '',
        account: ''
      },
      columns: [{
        title: '序号',
        width: 90,
        scopedSlots: { customRender: 'rn' }
      }, {
        width: 60,
        scopedSlots: { customRender: 'head' }
      }, {
        title: '销售姓名',
        dataIndex: 'name',
        ellipsis: true
      }, {
        title: '销售手机号码',
        dataIndex: 'account',
        ellipsis: true
      }, {
        title: '操作',
        width: 80,
        scopedSlots: { customRender: 'opt' }
      }],
      tableData: [],
      pagination: {},
      pageNum: 1,
      pageSize: 10,
      visible: false
    }
  },
  components: {
    [ChooseSalesPerson.name]: ChooseSalesPerson
  },
  methods: {
    handleSubmit1 () {
      this.pageNum = 1
      this.handleSubmit()
    },
    handleSubmit () {
      this.loading = true
      querySellUserListLand({
        ...this.searchForm,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(rsp => {
        this.loading = false
        this.tableData = rsp.data.list
        this.pagination = {
          current: this.pageNum,
          pageSize: rsp.data.pageSize,
          total: rsp.data.total,
          showQuickJumper: false,
          showSizeChanger: true,
          showTotal: total => `共${total}条记录 第${this.pageNum}/${rsp.data.pages}页`,
          onChange: pageNum => {
            this.pageNum = pageNum
            this.handleSubmit()
          },
          onShowSizeChange: (current, size) => {
            this.pageNum = current
            this.pageSize = size
            this.handleSubmit()
          }
        }
      }).catch(() => { this.loading = false })
    },
    handleAdd () {
      if (!this.$root._bc.create) {
        this.$message.error('您无此权限！')
        return
      }
      this.visible = true
    },
    removeIt (uiId) {
      if (!this.$root._bc.drop) {
        this.$message.error('您无此权限！')
        return
      }
      deleteSellExamine({ uiId, 'dataRole': 'LAND_SELL' }).then(rsp => {
        this.$message.success('移除成功')
        this.handleSubmit()
      })
    },
    setVisible (visible) {
      this.visible = visible
    },
    selectSalesPerson (userIds) {
      addSellExamine({ userIds, dataRole: 'LAND_SELL' }).then(rsp => {
        this.$message.success('添加成功')
        this.visible = false
        this.handleSubmit()
      })
    }
  },
  created () {
    this.handleSubmit()
  }
}
</script>

<style lang="less" scoped>
</style>
